package com.zjzywl.smoke.controller;

import com.zjzywl.smoke.config.AppConfig;
import com.zjzywl.smoke.entity.*;
import com.zjzywl.smoke.util.RestResult;
import com.zjzywl.smoke.util.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/answering")
public class AnsweringDeviceController extends BaseController {
    @Autowired
    private AppConfig appConfig;
    //获取未处理协同条数，协同数量，总报警数
    @RequestMapping(value = "/getNumData", method = RequestMethod.GET)
    public WebResult getNotice(@RequestParam("devCode") String devCode) {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/answering/getNumData?devCode="+devCode, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }

    }

    //获取报警列表
    @GetMapping("/getAlarms")
    public WebResult getAlarms(@RequestParam("devCode") String devCode) {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/answering/getAlarms?devCode="+devCode, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }

    }
    //获取协同列表
    @GetMapping("/getCollaboratives")
    public WebResult getInform(@RequestParam("devCode") String devCode) {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/answering/getCollaboratives?devCode="+devCode, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }

    /*
    一键报警
     */
    @GetMapping("/oneClickAlarm")
    public WebResult oneClickAlarm(@RequestParam("devCode") String devCode) {
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            String strDate = sdf.format(date);
            System.out.println(appConfig.getGetDataIp() + "/deviceStatus/push/apk/"+devCode+"1/"+strDate);
           appConfig.restTemplate()
                    .exchange(appConfig.getGetDataIp() + "/deviceStatus/push/apk/"+devCode+"/81/"+strDate, HttpMethod.GET, null, String.class);
            return WebResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }
    /*
     * 获取目标点的消防栓，应答器设备(协同操作)
     */
    @GetMapping(value = "/getPeripheralEquipment")
    public WebResult getPeripheralEquipment(@RequestParam(value = "devCode", required = false) String devCode) throws Exception {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/answering/getPeripheralEquipment?devCode="+devCode, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }

    /*
     * 添加,修改任务单(这个是平台下行发给apk的调度命令)
     */
    @PostMapping(value = "/save")
    public WebResult add(@RequestBody WorkOrder workOrder, @PathParam("orgid") Integer orgid)
            throws IOException {

        try {
            ResponseEntity<WorkOrder> result = appConfig.restTemplate().postForEntity(
                    appConfig.getGetDataIp() + "/answering/save?orgid="+orgid, workOrder, WorkOrder.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }
    //查找聊天信息
    @RequestMapping("/findByUserIdAndFriendId")
    public WebResult findByUserIdAndFriendId(@RequestParam("userid") String userid,@RequestParam("friendid") String friendid) {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/chatrecord/findByUserIdAndFriendId?userid="+userid+"&friendid="+friendid, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }

    /*
     * 获取目标点的重点人员(协同操作)
     */
    @GetMapping(value = "/getKeyPersonnel")
    public WebResult getKeyPersonnel(

            @RequestParam(value = "devCode", required = false) String devCode) throws Exception {
        try {
            ResponseEntity<Object> result = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/answering/getKeyPersonnel?devCode="+devCode, Object.class);
            return WebResult.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.getMessage());
        }
    }


}
